<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

// 使用类

use App\Http\Controllers\Controller;

// 后台订单控制器
class OrdersController extends Controller
{
    // 订单首页

    public function index(Request $request){

    	$count = $request -> input('count',10);
        $search = $request -> input('search','');
        $request = $request -> all();


        // 查询相关数据
    	 $data=\DB::table("orders")
                ->select("orders.*","userdetails.nickname","orderstatu.name as ssname")
                ->join("userdetails","userdetails.uid","=","orders.uid")
                ->join("orderstatu","orders.sid",'=',"orderstatu.id")
                ->where("orders.code",'like','%'.$search.'%')
                ->groupBy('orders.code')
                ->paginate($count);

                //dd($data);


        return view("admin.orders.index")->with('data',$data)->with('request',$request);

    }

   
    // 查看订单详情

    public function lists(Request $request)
    {

        // 获取订单号

        $code=$request->input("code");

        // 查询订单号下所有的商品

        $data=\DB::table("orders")
        		->select("orders.*","goods.goodsname","goods.img")
        		->join("goods","goods.id","=","orders.gid")
        		->where("code",$code)->get();

        // 数据展示到界面
        	//dd($data);
        return view("admin.orders.list")->with("data",$data);

    }


     // 收货地址方法

    public function addr($id)
    {
        // 获取数据

        // 查询订单收货地址信息

        $data=\DB::table("addr")->find($id);
        // dd($data);
        // 加载页面

        return view("admin.orders.addrUser")->with("data",$data);
    }



    // 订单状态列表的方法

    public function statuList()
    {
        // 查询数据

        $data=\DB::table('orderstatu')->get();

        // 加载页面

        return view("admin.orders.statuList")->with("data",$data);
    }

    // 订单状态的修改

    public function statuEdit(Request $request)
    {

        $name=$request->input('name');
        $id=$request->input('id');

       
        //$sql="update orderstatu set name='$name' where id=$id";

        // 修改数据

        if (\DB::table('orderstatu')->where('id',$id)->update(['name' => $name])) {
            # code...
            return 1;
        }else{
            return 0;
        }
    }



    public function edit(Request $request)
    {

        //echo 1;
        if ($_POST) {
                    # code...

                $sid=$request->input("sid");
                $code=$request->input("code");

                $sql="update orders set sid=$sid where code='$code'";

                // 执行sql语句

                if (\DB::update($sql)) {
                    # code...

                    return redirect("admin/orders");
                }else{
                    return back();
                }
            }else{

                // 查询所有的订单状态

                $data=\DB::table("orderstatu")->get();
                
               return view("admin.orders.edit")->with("data",$data);
            }
        }

}
